﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace App
{
    public partial class TeamStatistics : System.Web.UI.Page
    {
        AppLib.Team team;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Common.GetLoggedUser(this.Page) == null)
            {
                Response.Redirect("Login.aspx");
                return;
            }

            int id = int.Parse(Server.HtmlEncode(Request.Cookies["TeamFin"].Value));
            team = MidApp.ManageTeam.GetTeamByID(id);
            ImageAvatar2.DefaultImage = team.ImageURL;
            ImageAvatar2.Email = team.TeamID.ToString();
            LabelTeamName.Text = team.TeamName;
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            RepeaterTeamMembers.DataSource = MidApp.ManageIsMember.GetAllMembers(team.TeamID);
            RepeaterTeamMembers.DataBind();
        }
        protected string GetEventCredits()
        {
            var aux = MidApp.ManageTeam.GetTeamByTeamName(LabelTeamName.Text);
            List<AppLib.Participate> participate = MidApp.ManageParticipate.GetTeamParticipation(aux.TeamID);

            int userPrice = 0;
            AppLib.Event ev;
            foreach (var item in participate)
            {
                ev = MidApp.ManageEvent.GetEventByID(item.EventID);
                try
                {
                    if (ev.DateOfHolding > DateTime.Parse(txtBoxFrom.Text) && ev.DateOfHolding < DateTime.Parse(txtBoxTo.Text))
                    {
                        userPrice = userPrice + (int)ev.Price;
                    }
                }
                catch (Exception)
                {
                    userPrice = userPrice + (int)ev.Price;
                }

            }
            return userPrice.ToString();
        }
        protected string GetEventCredit(object sender)
        {
            var aux = (AppLib.IsMember)sender;

            List<AppLib.Participate> participate = MidApp.ManageParticipate.GetUserParticipationInTeam(aux.TeamID, aux.UserID);

            int userPrice = 0;
            AppLib.Event ev;
            foreach (var item in participate)
	        {
                ev = MidApp.ManageEvent.GetEventByID(item.EventID);
                try
                {
                    if (ev.DateOfHolding > DateTime.Parse(txtBoxFrom.Text) && ev.DateOfHolding <= DateTime.Parse(txtBoxTo.Text))
                    {
                        userPrice = userPrice + (int)ev.Price;
                    }
                }
                catch (Exception)
                {
                    userPrice = userPrice + (int)ev.Price;
                }
               
	        }
            return userPrice.ToString();
                    
        }

        protected string GetMemberCredit(object sender)
        {
            var aux = (AppLib.IsMember)sender;

            return MidApp.ManageStock.GetPlayer(aux.TeamID, aux.UserID).Credit.ToString();
        }
        protected string GetMemberCredits()
        {
            var aux = MidApp.ManageTeam.GetTeamByTeamName(LabelTeamName.Text);

            int amount = 0;
            var v = MidApp.ManageStock.GetPlayers(aux.TeamID);

            foreach (var item in v)
	        {
                amount = amount + (int)item.Credit;
	        }
            return amount.ToString();
            
        }
        protected string GetMemberNick(object sender)
        {
            var aux = (AppLib.IsMember)sender;

            return MidApp.ManageUser.GetUserByID(aux.UserID).Nick;
        }
        protected string GetLeaderID(object sender)
        {
            var aux = (AppLib.IsMember)sender;

            return aux.UserID.ToString();
        }
        protected string GetMemberAvatar(object sender)
        {
            try
            {
                var pom = (AppLib.IsMember)sender;
                return MidApp.ManageUser.GetUserImage(pom.UserID);
            }
            catch (InvalidCastException)
            {
                var pom = (AppLib.PendingRequest)sender;
                return MidApp.ManageUser.GetUserImage(pom.UserID);
            }

        }
        protected string GetMemberEmail(object sender)
        {
            try
            {
                var pom = (AppLib.IsMember)sender;
                return (MidApp.ManageUser.GetUserByID(pom.UserID)).PrimaryEmail;
            }
            catch (InvalidCastException)
            {
                var pom = (AppLib.PendingRequest)sender;
                return (MidApp.ManageUser.GetUserByID(pom.UserID)).PrimaryEmail;
            }

        }
        protected void LinkButtonUserInfo_Click(object sender, EventArgs e)
        {
            Response.Cookies["UserDetail"].Value = ((LinkButton)sender).CommandArgument;
            Response.Redirect("UserDetail.aspx");
        }

        protected void filterButton_Click(object sender, EventArgs e)
        {
            Page_PreRender(sender, e);
        }
    }
}